package com.nbjtjc.safe.mapper.emg;

import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.util.MyMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Mapper
public interface EmgPlanPublicMapper extends MyMapper<EmgPlanPublic>  {

    @Select("select path_id from emg_plan_public where id=#{planPublicId}")
    String getPathId(@Param("planPublicId") int planPublicId);

    List<EmgPlanPublic> search(@Param("epp") EmgPlanPublic emgPlanPublic, @Param("page") Page page);
    @Update("update emg_plan_public set status=#{status} where id=#{id}")
    int updateStatus(@Param("id") int id,@Param("status") int status);

    /**
     * 更新不是历史发布的应急预案的状态，更新的是调整后的预案的状态
     * @param id
     * @param status
     * @return
     */
    @Update("update emg_plan_public set status=#{status} where start_plan_id=#{id} and history=0")
    int updateStatusByStartPlanId(@Param("id") int id,@Param("status") int status);
    @Update("update emg_plan_public set status=#{status},finish=1 where id=#{id}")
    int finish(@Param("id") int id,@Param("status") int status);
    @Select("select start_plan_id from emg_plan_public where id=#{planPublicId}")
    int getStartPlanPublicId(@Param("planPublicId") int planPublicId);

    @Update("update emg_plan_public set history=1 where id=#{emgPlanPublic}")
    int setHistory(@Param("emgPlanPublic") int emgPlanPublic);
    /**
     * 查找当前id的不是历史的emgPlanPublic
     * @param startPlanPublic
     * @return
     */
    @ResultMap("detailMap")
    @Select("select * from emg_plan_public where start_plan_id=#{startPlanPublicId} and history=0")
    EmgPlanPublic getRunningEmgPlanPublic(@Param("startPlanPublicId") int startPlanPublic);
    /**
     * 查找当前id的不是历史的emgPlanPublic,且状态是status值的
     * @param startPlanPublic
     * @return
     */
    @ResultMap("detailMap")
    @Select("select * from emg_plan_public where start_plan_id=#{startPlanPublicId} " +
            "and history=0 and emg_plan_public.status=#{status}")
    EmgPlanPublic getRunningEmgPlanPublicByStatus(@Param("startPlanPublicId") int startPlanPublic
            ,@Param("status") int status);
    @Select("<script>"+
            "select count(*) from" +
            " emg_plan_public where company_id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach> " +
            " and history=0 " +
            " and create_date <![CDATA[ >= ]]> #{startDate} and create_date <![CDATA[ <= ]]> #{endDate}"
            + "</script>")
    int selectEmgPlanPublicCountByDeptIds(@Param("deptIds") List<Integer> deptIds, @Param("startDate") Date startDate
            , @Param("endDate") Date endDate);

    @Select("select company_id from emg_plan_public where id=#{planPublicId}")
    int getCompanyId(@Param("planPublicId") int planPublicId);

    @Select("select * from emg_plan_public where path_id=#{pathId} and history=#{history} " +
            " and company_id=#{companyId}")
    EmgPlanPublic getPlanPublicByPathId(@Param("pathId") String pathId
            , @Param("history") boolean history
            ,@Param("companyId") int companyId);
}